import { Good } from "../../components/goods/goods";
import { CARTS, COLLECTIONS, FOOTPRINTS } from "../../config";
import { Event } from "../../utils/interface";
import { observer } from "../../utils/observer";
import { Detail } from "../goods_detail/goods_detail";

let cols: Array<Detail> = [];
let cars: Array<Detail> = [];
let foos: Array<Detail> = [];
let tabs = ["商品收藏", "品牌收藏", "店铺收藏", "浏览足迹"];
let tabActive = tabs[0];
let status = ["全部", "正在热卖", "即将上线"];
let stateAcitve = status[0];
let arr: Array<Detail> = [];
let data = {
  cols,
  cars,
  foos,
  tabs,
  tabActive,
  status,
  stateAcitve,
  arr,
};
Page({
  data,
  onLoad() {
    data = observer(this.data, this) as any;
    this.init();
  },
  init() {
    data.arr = data.cols = wx.getStorageSync(COLLECTIONS);
    data.cars = wx.getStorageSync(CARTS);
    data.foos = wx.getStorageSync(FOOTPRINTS);
  },
  changeTabActive(e: Event<{ tab: string }>) {
    const str = e.currentTarget.dataset.tab;
    switch (str) {
      case "浏览足迹":
        data.arr = data.foos;
        break;
      case "商品收藏":
        data.arr = data.cols;
        break;
      default:
        break;
    }
    data.tabActive = str;
  },
  changeStateActive(e: Event<{ state: string }>) {
    const str = e.currentTarget.dataset.state;
    switch (str) {
      case "全部":
        data.arr = getCuurent();
        break;
      case "正在热卖":
        let newArr = [...data.arr];
        newArr.reverse();
        newArr = newArr.slice(newArr.length * 0.8);
        data.arr = newArr;
        break;
      case "即将上线":
        let newArr1 = [...data.arr];
        newArr1.reverse();
        newArr1 = newArr1.slice(newArr1.length * 0.5);
        data.arr = newArr1;
        break;
      default:
        break;
    }
    data.stateAcitve = str;
  },
  toDetails,
});
function toDetails(e: Event<{ row: Good }>) {
  wx.navigateTo({
    url: `/pages/goods_detail/goods_detail?goods_id=${e.currentTarget.dataset.row.goods_id}`,
  });
}
function getCuurent() {
  switch (data.tabActive) {
    case "商品收藏":
      return data.cols;
    case "浏览足迹":
      return data.foos;
  }
  return data.arr;
}
